//
//  BAHomeOriginalView.swift
//  BAweibo
//
//  Created by baggio on 2017/8/18.
//  Copyright © 2017年 baggio. All rights reserved.
//

import UIKit
import SnapKit

class BAHomeOriginalView: UIView {
    
    //保存约束
    private var bottomConstrain: Constraint?
    
    //提供一个属性供外界赋值
    //外界给 homeModel 赋值后，要给 originalView 中的各个控件赋值，暂时先做 headImageView 和 nameLabel
//    var homeModel: BAHomeModel? {
//        didSet{
//            headImageView.ba_sd_setImage(urlString: homeModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
//            nameLabel.text = homeModel?.user?.name
//            //如果要对数据添加业务逻辑，例如名字前面加“我叫”，放在这里不太好，因为这里是 view，不应该处理数据，mvc 的方式：可以在 model 中做
////            nameLabel.text = "我叫 \(homeModel?.user?.name ?? "")"
//        }
//    }
    //改成 mvvm 的方式，处理数据放到 homeViewModel 中
    var homeViewModel: BAHomeViewModel? {
        didSet{
            //头像
            headImageView.ba_sd_setImage(urlString: homeViewModel?.homeModel?.user?.profile_image_url, placeholderImageName: "avatar_default")
            //昵称
            nameLabel.text = homeViewModel?.homeModel?.user?.name
            //会员等级
            //封装到 viewModel 中
//            memeberLevelImageView.image = dealMbrankLevel(mbrank: homeViewModel?.homeModel?.user?.mbrank)
            memeberLevelImageView.image = homeViewModel?.mbrankImage
            //时间
            timeLabel.text = homeViewModel?.createTimeStr
            //来源
            sourceLabel.text = homeViewModel?.source
            
            //会员认证
            verifiedImageView.image = homeViewModel?.verifiedImage
            //正文
            contentLabel.text = homeViewModel?.homeModel?.text
            
            //配图
//            print(homeViewModel?.homeModel?.pic_urls)
            /*
             pic_urls.count > 0，代表有配图
                - 配图赋值
                - originalView.bottom = pictureView.bottom + BA_HOME_CELL_MARGIN
                - 显示配图
             pic_urls.count <= 0，代表没有配图
                - originalView.bottom = contentLabel.bottom + BA_HOME_CELL_MARGIN
                - 隐藏配图
             */
            
            //卸载约束
            bottomConstrain?.deactivate()
            //没有配图是个空数组，不是 nil
            if let pic_urls = homeViewModel?.homeModel?.pic_urls, pic_urls.count > 0 {
                //赋值
                pictureView.picUrls = pic_urls
                //约束
                self.snp.makeConstraints({ (m) in
                    bottomConstrain = m.bottom.equalTo(pictureView).offset(BA_HOME_CELL_MARGIN).constraint
                })
                //显示
                pictureView.isHidden = false
            } else {
                //约束
                self.snp.makeConstraints({ (m) in
                    bottomConstrain = m.bottom.equalTo(contentLabel).offset(BA_HOME_CELL_MARGIN).constraint
                })
                //隐藏
                pictureView.isHidden = true
            }
        }
    }
    
    //mbrank: Int = 0 //会员等级 1-6
    
    /*
     放到 homeViewModel 中
     放在这里有2个问题：
     1.处理数据的逻辑不应该放在 view，中，应该封装到 viewModel 中（viewModel 中添加一个属性 mbrankImage）
     2.滚动 tableView
        -> 就会执行 UITableViewDataSource 中的生成 cell 的方法
        -> 就会给 BAHomeCell 赋值
        -> 就会给 BAHomeOriginalView 赋值
        -> 就会执行这个方法
     导致这个方法会执行很多次，性能不好
     */
//    private func dealMbrankLevel(mbrank: Int?) -> UIImage? {
//        
//        print("处理会员等级业务逻辑")
//    
//        guard let m = mbrank else {
//            return UIImage(named: "common_icon_membership")
//        }
//        if m >= 1 && m <= 6 {
//            return UIImage(named: "common_icon_membership_level\(m)")
//        } else {
//            return UIImage(named: "common_icon_membership")
//        }
//    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setUpUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setUpUI() {
        //测试
        //backgroundColor = randomColor()
        backgroundColor = UIColor.white

        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(memeberLevelImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(verifiedImageView)
        addSubview(contentLabel)
        //做的时候先假设一定有 pictureView，后面再根据真实数据决定是否隐藏
        addSubview(pictureView)
        
        headImageView.snp.makeConstraints { (m) in
            m.left.top.equalTo(self).offset(BA_HOME_CELL_MARGIN)
            m.size.equalTo(BA_HOME_CELL_WH)
        }
        
        nameLabel.snp.makeConstraints { (m) in
            m.left.equalTo(headImageView.snp.right).offset(BA_HOME_CELL_MARGIN)
            m.top.equalTo(headImageView)
        }
        
        memeberLevelImageView.snp.makeConstraints { (m) in
            m.left.equalTo(nameLabel.snp.right).offset(BA_HOME_CELL_MARGIN)
            //图和文混着约束，一般用 center
            m.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp.makeConstraints { (m) in
            m.left.equalTo(nameLabel)
            m.top.equalTo(nameLabel.snp.bottom).offset(BA_HOME_CELL_MARGIN)
        }
        
        sourceLabel.snp.makeConstraints { (m) in
            m.left.equalTo(timeLabel.snp.right).offset(BA_HOME_CELL_MARGIN)
            m.bottom.equalTo(timeLabel)
        }
        
        verifiedImageView.snp.makeConstraints { (m) in
            m.centerX.equalTo(headImageView.snp.right)
            m.centerY.equalTo(headImageView.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { (m) in
            m.left.equalTo(headImageView)
            m.top.equalTo(headImageView.snp.bottom).offset(BA_HOME_CELL_MARGIN)
            
//            m.right.equalTo(self).offset(-BA_HOME_CELL_MARGIN)
            m.width.equalTo(BA_SCREEN_WIDTH - 2 * BA_HOME_CELL_MARGIN)
        }
        
        //配图
        pictureView.snp.makeConstraints { (m) in
            m.left.equalTo(contentLabel)
            m.top.equalTo(contentLabel.snp.bottom).offset(BA_HOME_CELL_MARGIN)
            //临时给个大小测试用
            m.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        // 自己
        self.snp.makeConstraints { (m) in
            //m.bottom.equalTo(contentLabel).offset(BA_HOME_CELL_MARGIN)
            //加了配图后
            bottomConstrain = m.bottom.equalTo(pictureView).offset(BA_HOME_CELL_MARGIN).constraint
        }
    }

    
    //头像 图
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    
    //昵称
    private lazy var nameLabel: UILabel = UILabel(textAlignment: nil, text: "昵称", textColor: UIColor.darkGray, fontSize: 14, numberOfLines: nil, alpha: nil)
    
    //会员等级 图
    private lazy var memeberLevelImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    
    //时间
    private lazy var timeLabel: UILabel = UILabel(textAlignment: nil, text: "时间", textColor: BA_COLOR_APP_THEME, fontSize: 10, numberOfLines: nil, alpha: nil)
    
    //来源
    private lazy var sourceLabel: UILabel = UILabel(textAlignment: nil, text: "来源", textColor: UIColor.darkGray, fontSize: 10, numberOfLines: nil, alpha: nil)
    
    //会员认证 图
    private lazy var verifiedImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    
    //正文
    private lazy var contentLabel: UILabel = UILabel(textAlignment: nil, text: "微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文微博正文", textColor: UIColor.black, fontSize: 14, numberOfLines: 0, alpha: nil)
    
    //配图
//    private lazy var pictureView: BAHomePictureView = BAHomePictureView()
    //配图使用 originalView 或 originalView 的背景色（可以使用 透明色，但是透明色性能不好，一般只用于图片只渲染一次的情况）
    private lazy var pictureView: BAHomePictureView = {
        let p = BAHomePictureView()
        p.backgroundColor = self.backgroundColor //self. 不会联想
        return p
    }()
}
